

import 'package:ycbl/common/my_course_type.dart';

/// 我的课程列表
class MyCourseListEntity {
  int ret;
  String msg;
  MyCourseRowsData data;

  MyCourseListEntity({this.ret, this.data, this.msg});

  MyCourseListEntity.fromJson(Map<String, dynamic> json, MyCourseType courseType) {
    ret = json['ret'];
    msg = json['msg'];
    data = json['data'] != null ? new MyCourseRowsData.fromJson(json['data'], courseType) : null;
  }

}

class MyCourseRowsData {
  int total;
  List<MyCourseEntity> rows;

  MyCourseRowsData({this.total, this.rows});

  MyCourseRowsData.fromJson(Map<String, dynamic> json, MyCourseType courseType) {
    total = json['total'];
    if (json['rows'] != null) {
      rows = new List<MyCourseEntity>();
      json['rows'].forEach((v) {
        rows.add(new MyCourseEntity.fromJson(v, courseType));
      });
    }
  }

}

/// 我的课程 item 数据
class MyCourseEntity {
  /// 课程名称
  String className;
  /// 课程主键 id，加入学习后的主键
  int mainId;
  /// 课时总数
  int courseNum;
  /// 已学课时
  int finishNum;
  /// 课程状态
  String status;
  /// 所属科目分类
  String kemu;
  /// 订单编号
  String orderNum;
  /// 老师头像
  String teacherPic;
  /// 老师姓名
  String teacherName;
  /// 结课日期
  String endTime;
  /// 开课日期
  String startTime;
  ///报名人数
  int studentNum;
  /// 分组类型 班课 or 一对一
  MyCourseType myCourseType;

  String subDesc = '';

  MyCourseEntity({
    this.className,
    this.mainId,
    this.courseNum,
    this.status,
    this.endTime,
    this.finishNum,
    this.kemu,
    this.orderNum,
    this.teacherPic,
    this.startTime,
    this.teacherName,
    this.myCourseType,
  });

  MyCourseEntity.fromJson(Map<String, dynamic> json, MyCourseType courseType) {
    //字段值 = 班课字段 ?? 一对一字段

    className = json['CLASS_NAME'];
    mainId = json['PK_STU_CLASS'];
    status = json['DIC_STATUS'];
    //用户购买的课时数还能是小数 ？？ 日 ！！！
    var buyCount = json['COURSENUM'] ?? json['BUY_CLASS_NUM'];
    courseNum = buyCount is double ? buyCount.toInt() : buyCount;
    finishNum = json['FINISHNUM'] ?? json['USED_CLASS_NUM'];
    kemu = json['KEMU'];
    orderNum = json['ORDER_NUM'];
    teacherPic = json['PIC'];
    teacherName = json['TEACHER'] ?? '百利名师';
    startTime = json['STARTTIME'];
    endTime = json['ENDTIME'];
    studentNum = json['S_NUM'];
    myCourseType = courseType;
    subDesc = _formatSubDesc();
  }

  String _formatSubDesc() {
    String desc = '';

    if (startTime != null && endTime != null) {
      desc = '$startTime 至 $endTime  ';
    }

    if (courseNum != null) {
      desc = '$desc共$courseNum课时';
    }

    return desc;
  }

  double progress() {
    if (courseNum == null || finishNum == null) {
      return 0;
    }else if (courseNum == 0) { //除 0 操作
      return 0;
    }else {
      return finishNum / courseNum;
    }
  }
}
